/**
 * @file
 * @auth jinguangguo
 * @date 2016/10/26
 */

import Vue from 'vue';
import moment from 'moment';
import Loading from '../../../../grsc-base/component/Loading.vue';
import NoData from '../../../../grsc-base/component/NoData.vue';
import tpl from './tpl.html';
import service from '../service';
import {PAGE_SIZE} from '../../../../grsc-base/common/constants';

export default Vue.extend({
    template: tpl,
    components: {
        loading: Loading,
        noData: NoData
    },
    data() {
        return {
            moment: moment,
            noticeId: this.$route.params.noticeId,
            isLoading: false,
            detail: null,
            title:'',
            condition: {
                pageNo: 1,
                pageSize: PAGE_SIZE,
                topStatus: ''   // 状态, YES:置顶, NO:不置顶
            },
            pageNum: 0,
            list:[],
            active: true,
        }
    },
    computed: {

    },
    created() {
        this.fetchData();
    },
    methods: {
        //切换公告
        avtiveNotice(obj){
            this.noticeId = obj.id;
            this.title = '';
            if(obj.content){
                this.detail = obj.content;
                return false;
            }
            service.noticeDetail({
                id: this.noticeId 
            }).then(rep => {
                this.isLoading = false;
                let detail = rep.data;
                detail.contentHtml = getFormattedContentHtml(detail.content);
                this.detail = detail;
                obj.content = this.detail;
            });
            
        },

        getNotice(type) {
            if(type == 'notice') {
                this.active = true;
                this.fetchData();
            } else {
                this.active = false;
                this.fetchData1();
            }
        },

        /**
         * 获取数据
         */
        fetchData() {
            this.isLoading = true;
            let param = {
                pageNo: this.condition.pageNo,
                pageSize: this.condition.pageSize
            };
            if (this.condition.topStatus !== '') {
                param.topStatus = this.condition.topStatus;
            }
            service.noticeList1(param).done((rep) => {
                this.isLoading = false;
                if (rep.code == window.CODE.SUCCESS) {
                    this.list = rep.data.list;
                    if(this.$route.query.id){
                        this.noticeId = this.$route.query.id
                    }else {
                        this.noticeId = rep.data.list[0].id;
                    }             
                    this.pageNum = rep.data.pageNum;
                    service.noticeDetail({
                        id: this.noticeId
                    }).then(rep => {
                        this.isLoading = false;
                        let detail = rep.data;
                        detail.contentHtml = getFormattedContentHtml(detail.content);
                        this.detail = detail;
                    });
                } else {
                    window.error(rep.msg);
                }
            }).fail((rep) => {

            });
        },
        fetchData1() {
            this.isLoading = true;
            this.detail = null;
            let param = {
                pageNo: this.condition.pageNo,
                pageSize: this.condition.pageSize
            };
            if (this.condition.topStatus !== '') {
                param.topStatus = this.condition.topStatus;
            }
            service.noticeList2(param).done((rep) => {
                this.isLoading = false;
                if (rep.code == window.CODE.SUCCESS) {  
                    this.list = rep.data.list;
                    console.log(this.list)
                    if(this.$route.query.id){
                        this.noticeId = this.$route.query.id
                    }else {
                        if(rep.data.list[0]) {
                            this.noticeId = rep.data.list[0].id;
                            this.pageNum = rep.data.pageNum;
                            service.noticeDetail({
                                id: this.noticeId
                            }).then(rep => {
                                this.isLoading = false;
                                let detail = rep.data;
                                detail.contentHtml = getFormattedContentHtml(detail.content);
                                this.detail = detail;
                            });
                        }
                    }             
                   
                } else {
                    window.error(rep.msg);
                }
            }).fail((rep) => {

            });
        }

    }
});

function getFormattedContentHtml(content) {
    let arrs = content.split('\n');
    let results = [];
    $.map(arrs, (text, index) => {
        text = $.trim(text);
        results.push(`<p class="text">${text}</p>`);
    });
    return results.join('');
}